using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Acme.BookStore.Domain.Entities;
using Acme.BookStore.Products.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Products
{
    //[Authorize]
    public class ShopWhereAppService : ApplicationService, IShopWhereAppService
    {
        private readonly IShopWhereRepository _shopWhereRepository;

        public ShopWhereAppService(IShopWhereRepository shopWhereRepository)
        {
            _shopWhereRepository = shopWhereRepository;
        }

        [HttpGet("api/app/shop-where/get-by-codes")]
        public async Task<ShopWhereDto> GetAsync(string ownerCode, string productCode, string productSku)
        {
            var shopWhere = await _shopWhereRepository.FindAsync(s => 
                s.OwnerCode == ownerCode && 
                s.ProductCode == productCode && 
                s.ProductSku == productSku);
                
            return ObjectMapper.Map<ShopWhere, ShopWhereDto>(shopWhere);
        }

        public async Task<PagedResultDto<ShopWhereDto>> GetListAsync(PagedAndSortedResultRequestDto input)
        {
            var totalCount = await _shopWhereRepository.GetCountAsync(null);

            var shopWheres = await _shopWhereRepository.GetListAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "ProductName",
                null);

            var shopWhereDtos = ObjectMapper.Map<List<ShopWhere>, List<ShopWhereDto>>(shopWheres);

            return new PagedResultDto<ShopWhereDto>(totalCount, shopWhereDtos);
        }

        [HttpGet("api/app/shop-where/search")]
        public async Task<PagedResultDto<ShopWhereDto>> GetListAsync(ShopWhereSearchDto input)
        {
            var totalCount = await _shopWhereRepository.GetCountAsync(
                input.Filter,
                input.OwnerCode,
                input.ProductCode,
                input.ProductName
            );

            var shopWheres = await _shopWhereRepository.GetListAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "ProductName",
                input.Filter,
                input.OwnerCode,
                input.ProductCode,
                input.ProductName
            );

            var shopWhereDtos = ObjectMapper.Map<List<ShopWhere>, List<ShopWhereDto>>(shopWheres);

            return new PagedResultDto<ShopWhereDto>(totalCount, shopWhereDtos);
        }
        
        [HttpGet("api/app/shop-where/search-by-name")]
        public async Task<PagedResultDto<ShopWhereDto>> SearchByNameAsync(ShopWhereNameSearchDto input)
        {
            var totalCount = await _shopWhereRepository.GetCountByNameAsync(input.NameKeyword);

            var shopWheres = await _shopWhereRepository.SearchByNameAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "ProductName",
                input.NameKeyword
            );

            var shopWhereDtos = ObjectMapper.Map<List<ShopWhere>, List<ShopWhereDto>>(shopWheres);

            return new PagedResultDto<ShopWhereDto>(totalCount, shopWhereDtos);
        }
    }
} 